package main

import (
	"api/utils"
	"fmt"
	"reflect"

	"github.com/beego/beego/v2/core/validation"
	b "github.com/beego/beego/v2/server/web"
)

type Test struct {
	Name string
	Age  uint32
	Sex  bool
}

// 遍历结构体，但是这里的结构体不是结构体本身，而是一个指向结构体的指针，所以使用"Elem"方法来获取指针指向的结构体。
func eachStruct() {
	typ := reflect.TypeOf(b.BConfig).Elem()
	val := reflect.ValueOf(b.BConfig).Elem()
	for i := 0; i < typ.NumField(); i += 1 {
		field := typ.Field(i)
		value := val.Field(i)
		fmt.Printf("%s: %v\n", field.Name, value)
	}
}

func f(data ...uint32) {
	fmt.Println(len(data), data[0]+1)
}
func f2(params Test) {
	// 默认值设定
	if params.Name == "" {
		params.Name = "木头七"
	}
	if params.Age == 0 {
		params.Age = 37
	}
	fmt.Println(params)
}

func test(value int) string {
	valid := validation.Validation{}
	valid.Range(value, 0, 365, "value")
	if valid.HasErrors() {
		item := valid.Errors[0]
		msg := utils.Translate(item)
		return msg
	}

	return ""
}

func main() {
	//eachStruct()

	//f(2345)

	//f2(Test{"张三", 41, true})
	//f2(Test{Name: "李四", Age: 34, Sex: false})
	//f2(Test{})
	//f2(Test{Name: "王五"})
	//f2(Test{Age: 14})

	//var t utils.MyTime
	//fmt.Println(t.Time)
	//fmt.Println(t.Time.Format("2006-01-02 15:04:05"))
	//day := 365
	//errMsg := test(day)
	//if errMsg != "" {
	//	println(errMsg)
	//} else {
	//	println("验证通过")
	//}

	//err := MyRedis.InitRedis()
	//if err != nil {
	//	println(err.Error())
	//	return
	//}
	//
	//key := "admin_user_2"
	//value, errMsg := MyRedis.GetData(key)
	//if errMsg != "" {
	//	println(errMsg)
	//	return
	//}
	//
	//println("[" + value + "]")
	//
	//dura, errMsg := MyRedis.GetExpire(key)
	//if errMsg != "" {
	//	println(errMsg)
	//	return
	//}
	//println(dura)

	//ip := "192.168.6.104:1234"
	//i := strings.LastIndex(ip, ":")
	//a := ip[0:13]
	//println(a)

	//type User struct {
	//	UserName string `valid:"Required; MinSize(4); MaxSize(20);"`
	//	Password string `valid:"Required; Length(16);"`
	//}
	////user := User{UserName: "abc", Password: "123456"}
	//user := map[string]interface{}{}
	//user["UserName"] = "abc"
	//user["Password"] = "123456"
	//valid := validation.Validation{}
	//isOk, err := valid.Valid(user)
	//if err != nil {
	//	println(err.Error())
	//	return
	//}
	//if isOk {
	//	println("ok")
	//	return
	//}
	//println(utils.Translate(valid.Errors[0]))

	//data := map[string]interface{}{}
	//data["id"] = 4
	//data["menu"] = "12,13,1"
	//data["name"] = "测试员33"
	//data["res"] = "120,110,123"
	//fmt.Println(data)

	//var a byte = 65
	//aa := utils.ToStr(a)
	//println("[" + aa + "]")

	a := "d:/workspace/bottleAdmin/dist"
	println(a[2:])
}
